package com.youlv.yl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlv.yl.bo.ShopCategoryBo;
import com.youlv.yl.config.RedisKeyConfig;
import com.youlv.yl.dao.ShoppingDao;
import com.youlv.yl.entity.shopping.ShopCome;
import com.youlv.yl.service.intf.ShoppingService;
import com.youlv.yl.util.JedisUtils;
import com.youlv.yl.vo.R;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShopingServiceImpl extends ServiceImpl<ShoppingDao,ShopCome> implements ShoppingService {
    @Autowired
    private ShoppingDao shoppingDao;

    @Override
    public R getAllShops(String sname) {
        if (sname ==null){
            return R.ok(shoppingDao.getAllShops());
        }
        if (sname != null){
            if(shoppingDao.searchShop(sname).size()==0){
                R r = new R(400,shoppingDao.getAllShops(),"没有符合的商店，为您推荐以下商店");
                return r;
            }else {
                return R.ok(shoppingDao.searchShop(sname));
            }

        }

        return null;
    }

    @Override
    public R getOneShop(String id) {

        System.out.println(shoppingDao.getOneShop(id));
        return R.ok(shoppingDao.getOneShop(id));
    }

    @Override
    public R countGeoDistance(ShopCategoryBo bo) {
        List<ShopCategoryBo> allShops = shoppingDao.getAllShops();

        JedisUtils.addGeo(RedisKeyConfig.GEO_UCODE,Double.valueOf(bo.getLongitude()),Double.valueOf(bo.getLatitude()),"郑州");
        List<ShopCategoryBo> newShops = new ArrayList<>();
        for (ShopCategoryBo allShop : allShops) {

            if(allShop.getLocation().equals("郑州") && allShop.getLongitude()!=null){
                JedisUtils.addGeo(RedisKeyConfig.GEO_UCODE,Double.valueOf(allShop.getLongitude()),Double.valueOf(allShop.getLatitude()),allShop.getShopname());

                allShop.setDistance(String.valueOf(JedisUtils.geodist(RedisKeyConfig.GEO_UCODE, "郑州", allShop.getShopname())));
                newShops.add(allShop);
            }
        }

        JedisUtils.delKey(RedisKeyConfig.GEO_UCODE);
        JedisUtils.close();

        return R.ok(newShops);
    }

    @Override
    public R searchShop(String shopname) {
        return null;
    }
}
